package com.softtown.modules.organization.controller;

import com.github.pagehelper.PageInfo;
import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.common.validator.ValidatorUtils;
import com.softtown.common.validator.group.DefaultGroup;
import com.softtown.modules.organization.dto.*;
import com.softtown.modules.organization.entity.CompOrgEmplEntity;
import com.softtown.modules.organization.entity.CompOrgHstEmplEntity;
import com.softtown.modules.organization.service.CompOrgEmplPosnService;
import com.softtown.modules.organization.service.CompOrgEmplService;
import com.softtown.modules.organization.vo.CompOrgEmplTravelAndStudyVO;
import com.softtown.modules.sys.dto.SysUserDTO;
import com.softtown.modules.sys.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/6/6 08:51
 * @Description:
 */
@RestController
@RequestMapping("/comp/org")
@Api(tags = "员工管理")
public class CompOrgEmplController {

    @Resource
    private CompOrgEmplService compOrgEmplService;

    @Resource
    private CompOrgEmplPosnService compOrgEmplPosnService;

    /**
     * 查找用户角色
     *
     * @param valueId id值
     * @return com.softtown.common.utils.Result<java.util.List < java.lang.Long>>
     * @Author Dingyayong
     * @Date 2020/4/13 20:28
     */
    @PostMapping("findEmplUserRole")
    @ApiOperation("查找用户角色")
    @LogOperation("查找用户角色")
    public Result<List<Long>> findEmplUserRole(@RequestBody String valueId) {
        String userid = JSONObject.fromObject(valueId).get("id").toString();
        String compId = JSONObject.fromObject(valueId).get("compId").toString();
        List<Long> roleIdList = compOrgEmplService.findEmplUserRole(Long.valueOf(compId), Long.valueOf(userid));
        return new Result<List<Long>>().ok(roleIdList);
    }

    /**
     * 员工激活
     *
     * @param id id
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 20:29
     */
    @PostMapping("activate")
    @ApiOperation("员工激活")
    @LogOperation("员工激活")
    public Result activate(@RequestBody String id) {
        String emplId = JSONObject.fromObject(id).get("id").toString();
        compOrgEmplService.activeOrgEmpl(Long.valueOf(emplId));
        return new Result<>();
    }

    /**
     * 批准
     *
     * @param sysUserDTO 用户管理DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 20:30
     */
    @PostMapping("approveUserAppl")
    @ApiOperation("批准")
    @LogOperation("批准")
    public Result approveUserAppl(@RequestBody SysUserDTO sysUserDTO) {
        compOrgEmplService.approveUserAppl(sysUserDTO);
        return new Result<>();
    }

    /**
     * 查找用户
     *
     * @param valueId id值
     * @return com.softtown.common.utils.Result<com.softtown.modules.sys.vo.SysUserVO>
     * @Author Dingyayong
     * @Date 2020/4/13 20:31
     */
    @PostMapping("findEmplUser")
    @ApiOperation("查找用户")
    @LogOperation("查找用户")
    public Result<SysUserVO> findEmplUser(@RequestBody String valueId) {
        String emplId = JSONObject.fromObject(valueId).get("emplId").toString();
        String compId = JSONObject.fromObject(valueId).get("compId").toString();
        SysUserVO sysUserEntity = compOrgEmplService.findEmplUser(Long.valueOf(emplId), Long.valueOf(compId));
        return new Result<SysUserVO>().ok(sysUserEntity);
    }

    /**
     * @param @param [valueId]
     * @return @return com.softtown.common.utils.Result<java.util.List<java.lang.Long>>
     * @throws
     * @Title: findEmplPosns
     * @Description: 找出员工所在的岗位信息
     * @author fuming
     * @date 2019/7/4 16:28
     */
    @PostMapping("findEmplPosns")
    @ApiOperation("找出员工所在的岗位")
    @LogOperation("找出员工所在的岗位")
    public Result<List<Long>> findEmplPosns(@RequestBody String valueId) {
        String emplId = JSONObject.fromObject(valueId).get("emplNum").toString();
        String compId = JSONObject.fromObject(valueId).get("compId").toString();
        List<Long> posnIdList = compOrgEmplPosnService.getPosnIdByEmplIdAndCompId(Long.valueOf(emplId), Long.valueOf(compId));
        return new Result<List<Long>>().ok(posnIdList);
    }

    /**
     * @param @param [valueId]
     * @return @return com.softtown.common.utils.Result<java.util.List<java.lang.Long>>
     * @throws
     * @Title: findEmplDept
     * @Description: 找出员工所在的部门信息
     * @author fuming
     * @date 2019/7/4 17:06
     */
    @PostMapping("findEmplDepts")
    @ApiOperation("找出员工所在的部门")
    @LogOperation("找出员工所在的部门")
    public Result<List<Long>> findEmplDept(@RequestBody String valueId) {
        String emplId = JSONObject.fromObject(valueId).get("emplNum").toString();
        String compId = JSONObject.fromObject(valueId).get("compId").toString();
        List<Long> deptIdList = compOrgEmplService.getDeptIdByEmplIdAndCompId(Long.valueOf(emplId), Long.valueOf(compId));
        return new Result<List<Long>>().ok(deptIdList);
    }

    /**
     * @param @param [compOrgHstEmplDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: getEmplHstList
     * @Description: 获取员工变更履历分页
     * @author fuming
     * @date 2019/7/4 20:20
     */
    @PostMapping("getEmplHstList")
    @ApiOperation("获取员工履历信息分页")
    @LogOperation("获取员工履历信息分页")
    public Result getEmplHstList(@RequestBody CompOrgHstEmplDTO compOrgHstEmplDTO) {
        PageInfo<CompOrgHstEmplEntity> emplHstListByCompId = compOrgEmplService.getEmplHstListByCompId(compOrgHstEmplDTO);
        return new Result<PageInfo<CompOrgHstEmplEntity>>().ok(emplHstListByCompId);
    }

    /**
     * @param @param [compOrgEmplDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: addEmpl
     * @Description: 添加员工信息
     * @author fuming
     * @date 2019/7/5 22:40
     */
    @PostMapping("addEmpl")
    @ApiOperation("添加员工")
    @LogOperation("添加员工")
    public Result addEmpl(@RequestBody CompOrgEmplDTO compOrgEmplDTO) {
        Result result = new Result<>();
        //校验数据是否为空
        ValidatorUtils.validateEntity(compOrgEmplDTO, DefaultGroup.class);
        if (RegularUtils.isRealName(compOrgEmplDTO.getRealName())) {
            return result.error(ErrorCode.INCORRECT_REAL_NAME_FORMAT);
        }
        if (!RegularUtils.isMobile(compOrgEmplDTO.getMobile())) {
            return result.error(ErrorCode.INCORRECT_MOBILE_FORMAT);
        }
        if (!RegularUtils.isEmail(compOrgEmplDTO.getEmail())) {
            return result.error(ErrorCode.INCORRECT_EMAIL_FORMAT);
        }
        List<CompOrgEmplEntity> emplinfoList = compOrgEmplService.findByCompId(compOrgEmplDTO.getCompId());
        for (CompOrgEmplEntity emplEntity:emplinfoList) {
            String emplNum = emplEntity.getEmplNum();
            String mobile = emplEntity.getMobile();
            String email = emplEntity.getEmail();
            if (compOrgEmplDTO.getEmplNum().equals(emplNum)) {
                return result.error(ErrorCode.EMPLNUM_EXISTS);
            }
            if (compOrgEmplDTO.getMobile().equals(mobile)) {
                return result.error(ErrorCode.MOBILE_EXISTS);
            }
            if (compOrgEmplDTO.getEmail().equals(email)) {
                return result.error(ErrorCode.EMAIL_EXISTS);
            }
        }
        compOrgEmplService.addEmployee(compOrgEmplDTO);
        return result;
    }

    /**
     * @param @param [compOrgEmplDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: updateEmpl
     * @Description: 修改员工信息
     * @author fuming
     * @date 2019/7/6 9:43
     */
    @PostMapping("updateEmpl")
    @ApiOperation("修改员工")
    @LogOperation("修改员工")
    public Result updateEmpl(@RequestBody CompOrgEmplDTO compOrgEmplDTO) {
        Result result = new Result<>();
        if (RegularUtils.isRealName(compOrgEmplDTO.getRealName())) {
            return result.error(ErrorCode.INCORRECT_REAL_NAME_FORMAT);
        }
        if (!RegularUtils.isEmail(compOrgEmplDTO.getEmail())) {
            return result.error(ErrorCode.INCORRECT_EMAIL_FORMAT);
        }
        if (!RegularUtils.isMobile(compOrgEmplDTO.getMobile())) {
            return result.error(ErrorCode.INCORRECT_MOBILE_FORMAT);
        }
        List<CompOrgEmplEntity> emplinfoList = compOrgEmplService.findByCompId(compOrgEmplDTO.getCompId());
        for (CompOrgEmplEntity emplEntity:emplinfoList) {
            String emplNum = emplEntity.getEmplNum();
            String mobile = emplEntity.getMobile();
            String email = emplEntity.getEmail();
            Long id = emplEntity.getId();
            if (compOrgEmplDTO.getMobile().equals(mobile) && !compOrgEmplDTO.getId().equals(id)) {
                return result.error(ErrorCode.MOBILE_EXISTS);
            }
            if (compOrgEmplDTO.getEmplNum().equals(emplNum) && !compOrgEmplDTO.getId().equals(id)) {
                return result.error(ErrorCode.EMPLNUM_EXISTS);
            }
            if (compOrgEmplDTO.getEmail().equals(email) && !compOrgEmplDTO.getId().equals(id)) {
                return result.error(ErrorCode.EMAIL_EXISTS);
            }
        }
        compOrgEmplService.updateEmplInfos(compOrgEmplDTO);
        return result;
    }

    /**
     * @param @param [compOrgEmplDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: getEmployeeList
     * @Description: 员工信息查询和多表分页
     * @author fuming
     * @date 2019/7/6 10:21
     */
    @PostMapping("getEmployeeList")
    @ApiOperation("获取员工信息分页")
    @LogOperation("获取员工信息分页")
    public Result getEmployeeList(@RequestBody CompOrgEmplDTO compOrgEmplDTO) {
        PageInfo<CompOrgEmplEntity> employeeListByConditions = compOrgEmplService.getEmployeeListByConditions(compOrgEmplDTO);
        return new Result<PageInfo<CompOrgEmplEntity>>().ok(employeeListByConditions);
    }

    /**
     * @param @param [valueId]
     * @return @return com.softtown.common.utils.Result<java.util.List<com.softtown.modules.organization.entity.CompOrgEmplEntity>>
     * @throws
     * @Title: findEmplInfos
     * @Description: 查找员工信息
     * @author fuming
     * @date 2019/7/6 10:41
     */
    @PostMapping("findEmplInfos")
    @ApiOperation("查找公司员工")
    @LogOperation("查找公司员工")
    public Result<List<CompOrgEmplEntity>> findEmplInfos(@RequestBody String valueId) {
        String compId = JSONObject.fromObject(valueId).get("compId").toString();
        List<CompOrgEmplEntity> emplInfosByCompId = compOrgEmplService.findByCompId(Long.valueOf(compId));
        return new Result<List<CompOrgEmplEntity>>().ok(emplInfosByCompId);
    }

    /**
     * 根据员工id/用户id和公司id查找公司员工
     *
     * @param compOrgEmplByCompIdUserIdDTO 员工信息DTO
     * @return com.softtown.common.utils.Result
     * @Author fuming
     * @Date 2020/1/20 14:26
     */
    @PostMapping("getEmplInfo")
    @ApiOperation("员工id和公司id查找公司员工")
    @LogOperation("员工id和公司id查找公司员工")
    public Result getEmplInfo(@RequestBody CompOrgEmplByCompIdUserIdDTO compOrgEmplByCompIdUserIdDTO) {
        return compOrgEmplService.getEmplToTravlAndStudy(compOrgEmplByCompIdUserIdDTO);
    }

    /**
     * 根据用户公司查找公司员工
     *
     * @param compOrgEmplPageByCompIdUserIdDTO 根据用户id和公司id查找用户DTO
     * @return com.softtown.common.utils.Result
     * @Author fuming
     * @Date 2020/4/13 20:34
     */
    @PostMapping("getEmplInfoList")
    @ApiOperation("根据用户公司查找公司员工")
    @LogOperation("根据用户公司查找公司员工")
    public Result getEmplInfoList(@RequestBody CompOrgEmplPageByCompIdUserIdDTO compOrgEmplPageByCompIdUserIdDTO) {
        PageInfo<CompOrgEmplTravelAndStudyVO> emplListToTravlAndStudy = compOrgEmplService.getEmplListToTravlAndStudy(compOrgEmplPageByCompIdUserIdDTO);
        for (int i = 0; i < emplListToTravlAndStudy.getList().size(); i++) {
            if (compOrgEmplPageByCompIdUserIdDTO.getUserId().equals(emplListToTravlAndStudy.getList().get(i).getUserId())) {
                emplListToTravlAndStudy.getList().remove(i);
            }
        }
        return new Result<PageInfo<CompOrgEmplTravelAndStudyVO>>().ok(emplListToTravlAndStudy);
    }

    /**
     * 根据userId获取员工信息
     *
     * @param compOrgEmplByCompIdUserIdDTO 根据用户id和公司id查找用户DTO
     * @return Result
     * @author Dingyayong
     * @date 2019-08-30 15:43
     */
    @PostMapping("getEmplInfoByUserId")
    @ApiOperation("根据用户公司查找公司员工信息")
    @LogOperation("根据用户公司查找公司员工信息")
    public Result getEmplInfoByUserId(@RequestBody CompOrgEmplByCompIdUserIdDTO compOrgEmplByCompIdUserIdDTO) {
        return compOrgEmplService.getEmplInfoByUserId(compOrgEmplByCompIdUserIdDTO);
    }

    /**
     * 查找用户岗位
     *
     * @param compOrgPosnEmplPageDTO 用户员工岗位分页DTO
     * @return com.softtown.common.utils.Result
     * @Author fuming
     * @Date 2020/4/13 20:34
     */
    @PostMapping("getPagePosnEmplList")
    @ApiOperation("查找用户岗位")
    @LogOperation("查找用户岗位")
    public Result getPagePosnEmplList(@RequestBody CompOrgPosnEmplPageDTO compOrgPosnEmplPageDTO) {
        return new Result<>().ok(compOrgEmplPosnService.getPageOrgPosnEmpl(compOrgPosnEmplPageDTO));
    }

    /**
     * 获取员工的部门名称信息
     *
     * @param valueId
     * @return com.softtown.common.utils.Result<java.lang.String>
     * @author fuming
     * @date 2019/11/9 17:28
     */
    @PostMapping("getEmplDeptName")
    @ApiOperation("获取员工的部门名称信息")
    @LogOperation("获取员工的部门名称信息")
    public Result<String> getEmplDeptName(@RequestBody String valueId) {
        String compId = JSONObject.fromObject(valueId).get("compId").toString();
        String emplId = JSONObject.fromObject(valueId).get("emplId").toString();
        String emplDeptName = compOrgEmplService.selectEmplDeptName(Long.valueOf(compId), Long.valueOf(emplId));
        return new Result<String>().ok(emplDeptName);
    }

    /**
     * 获取公司的员工信息
     *
     * @param compOrgEmplDTO
     * @return com.softtown.common.utils.Result
     * @author fuming
     * @date 2019/11/25 20:04
     */
    @PostMapping("getEmplList")
    @ApiOperation("获取员工信息分页")
    @LogOperation("获取员工信息分页")
    public Result getEmplList(@RequestBody CompOrgEmplDTO compOrgEmplDTO) {
        PageInfo<CompOrgEmplEntity> compOrgEmplEntityPageInfo = compOrgEmplService.selectEmployeeList(compOrgEmplDTO);
        return new Result<PageInfo<CompOrgEmplEntity>>().ok(compOrgEmplEntityPageInfo);
    }
}
